/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.skills.mmoskills;

import jmmos.gameserver.datatables.NpcTable;
import jmmos.gameserver.datatables.SkillTable;
import jmmos.gameserver.idfactory.IdFactory;
import jmmos.gameserver.model.MMOObject;
import jmmos.gameserver.model.MMOSpawn;
import jmmos.gameserver.model.StatsSet;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.MMOTrap;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.model.actor.instance.MMOTrapInstance;
import jmmos.gameserver.model.actor.templates.MMONpcTemplate;
import jmmos.gameserver.model.skills.MMOSkill;

public class MMOSkillTrap extends MMOSkillSummon
{
	private int _triggerSkillId = 0;
	private int _triggerSkillLvl = 0;
	protected MMOSpawn _trapSpawn;
	
	/**
	 * @param set
	 */
	public MMOSkillTrap(StatsSet set)
	{
		super(set);
		_triggerSkillId = set.getInteger("triggerSkillId");
		_triggerSkillLvl = set.getInteger("triggerSkillLvl");
	}
	
	public int getTriggerSkillId()
	{
		return _triggerSkillId;
	}
	
	@Override
	public void useSkill(MMOCharacter caster, MMOObject[] targets)
	{
		if (caster.isAlikeDead() || !caster.isPlayer())
		{
			return;
		}
		
		if (getNpcId() == 0)
		{
			return;
		}
		
		MMOPcInstance activeChar = caster.getActingPlayer();
		
		if (activeChar.inObserverMode())
		{
			return;
		}
		
		if (activeChar.isMounted())
		{
			return;
		}
		
		if ((_triggerSkillId == 0) || (_triggerSkillLvl == 0))
		{
			return;
		}
		
		MMOTrap trap = activeChar.getTrap();
		if (trap != null)
		{
			trap.unSummon();
		}
		
		MMOSkill skill = SkillTable.getInstance().getInfo(_triggerSkillId, _triggerSkillLvl);
		
		if (skill == null)
		{
			return;
		}
		
		MMONpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(getNpcId());
		trap = new MMOTrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, activeChar, getTotalLifeTime(), skill);
		trap.setCurrentHp(trap.getMaxHp());
		trap.setCurrentMp(trap.getMaxMp());
		trap.setIsInvul(true);
		trap.setHeading(activeChar.getHeading());
		activeChar.setTrap(trap);
		trap.spawnMe(activeChar.getX(), activeChar.getY(), activeChar.getZ());
	}
}
